//radenwigirizkirama
package jtk.project4.fleet.screen.addNewFuelTank;

import java.awt.FlowLayout;
import java.util.List;

import jtk.project4.fleet.action.CancelAddFuelTank;
import jtk.project4.fleet.action.SaveAddNewFuelTank;
import jtk.project4.fleet.event.AddNewFuelTankEvent;
import jtk.project4.fleet.field.AddNewFuelTankField;

import nl.coderight.jazz.form.FormHeader;
import nl.coderight.jazz.form.FormView;
import nl.coderight.jazz.form.control.GroupControl;
import nl.coderight.jazz.form.field.button.PushButton;
import nl.coderight.jazz.form.validation.ValidationError;

public class AddNewFuelTankView extends FormView
{
	private FormHeader formHeader;
	private PushButton submitButton;
	private PushButton cancelButton;
	
	public void handleEvent(AddNewFuelTankEvent evt){
		switch (evt.getType()){
		case CANCEL:
		close();
		break;
		
		case SAVE:
		List<ValidationError> errors = validate();
		if (!errors.isEmpty()){
		errors.get(0).getField().focus();
		break;
		} else {
			submit();
			close();
		}
		propagateEvent(evt);
		break;
	}
}

	@Override
	protected void init() {
		createFields();
		createLayout();	
		setResizable(false);
	}	
	
	
	private void createFields(){
		submitButton = new PushButton(new SaveAddNewFuelTank());
		cancelButton = new PushButton(new CancelAddFuelTank());
	}
	
	private void createLayout() {
		setHeader(createHeader());
		addField(createForm());
	}
	
	private FormHeader createHeader() {
		formHeader = new FormHeader();
		formHeader.setTitle("Add New Fuel Tank.header.form");
		return formHeader;
	}
	
	private GroupControl createForm() 
	{
		GroupControl form = new GroupControl();
		form.addField(new AddNewFuelTankField("FuelTank"));
		form.addField(createButtons(), SOUTH);		
		return form;
	}
	
	private GroupControl createButtons() {
		GroupControl buttons = new GroupControl();
		buttons.setLayout(new FlowLayout(FlowLayout.CENTER));
		buttons.addField(submitButton);
		buttons.addField(cancelButton);
		return buttons;
	}
	
	

}